package com.itheima.sqlSession;

import com.itheima.executor.Executor;
import com.itheima.pojo.Configuration;
import com.itheima.pojo.MappedStatement;

import javax.sql.DataSource;
import java.lang.reflect.*;
import java.util.List;

public class DefaultSqlSession implements SqlSession{
    private Configuration configuration;
    private Executor executor;

    public DefaultSqlSession(Configuration configuration,Executor executor) {
        this.configuration = configuration;
        this.executor=executor;
    }

    public DefaultSqlSession() {
    }

    @Override
    public <E> List<E> selectList(String statementId, Object param) throws Exception {
        //query():执行底层的jdbc   1.数据库配置信息  2.sql配置信息
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
       List<E>list= executor.query(configuration,mappedStatement,param);
        return list;
    }

    @Override
    public <T> T selectOne(String statementId, Object param) throws Exception {
        List<Object> list = this.selectList(statementId, param);
        if(list.size()==1){
            return (T)list.get(0);
        }else if(list.size()>1){
            throw new RuntimeException("返回结果过多");
        }else {
            return null;
        }
    }

    @Override
    public void close() {
        executor.close();
    }

    @Override
    public <T> T getMapper(Class<?> mapperClass) {
        //使用jdk动态代理生成基于接口的代理对象
        Object proxy = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {
            @Override
            /**
             * proxy代理对象引用
             * method被代理的对象
             * args 调用的方法的参数
             */
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                //具体逻辑
                //通过调用sqlSession方法里的方法完成操作
                //参数的准备 1.statementId 2.param
                String methodName = method.getName();
                String className = method.getDeclaringClass().getName();
                String statementId=className+"."+methodName;
                //判断执行的方法的类型
                MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
                String sqlCommandType=mappedStatement.getSqlCommandType();
                switch (sqlCommandType){
                    case "select":
                        Type genericReturnType = method.getGenericReturnType();
                        //判断返回类型是否实现了泛型类型参数化  如果有的话代表返回类型是list
                        if(genericReturnType instanceof ParameterizedType){
                           return selectList(statementId,args==null?null:args[0]);
                        }
                        return selectOne(statementId,args[0]);
                    case "update":
                        break;
                    case "delete":
                        break;
                    case "insert":
                        break;
                    default:
                        throw new RuntimeException("调用方法有误");
                }
                return null;
            }
        });
        return (T) proxy;
    }
}
